#include <graphics.h>
#include <windows.h>

using namespace std;

// 地图高宽
#define Width 640
#define Height 480

#define WIDTH_RECTANGLE 10
#define HEIGHT_RECTANGLE 10
#define RADIUS_CIRCLE 10

typedef struct _Pos {
    int x;
    int y;
}Pos;

int main()
{
    bool isDrawLine = false;
    Pos firstPos;
    MOUSEMSG msg;           // 定义鼠标消息

    // 初始化
    initgraph(Width, Height);

    msg = GetMouseMsg();
    firstPos.x = msg.x;
    firstPos.y = msg.y;

    // 循环绘制
    while (true) {
        msg = GetMouseMsg();  // 获取鼠标消息
        if (msg.uMsg == WM_MOUSEMOVE) {
            // 1. 鼠标移动时绘制白色小点
            // putpixel(msg.x, msg.y, WHITE);
            
            // 2. 鼠标移动时绘制白色线（但离开界面再进入时，出现绘制连续问题）
            line(firstPos.x, firstPos.y, msg.x, msg.y);
            firstPos.x = msg.x;
            firstPos.y = msg.y;
        }
        else if (msg.uMsg == WM_LBUTTONDOWN) {
            // 鼠标左键点击时绘制一个矩形
            rectangle(msg.x - WIDTH_RECTANGLE / 2, msg.y - WIDTH_RECTANGLE / 2, msg.x + HEIGHT_RECTANGLE / 2, msg.y + HEIGHT_RECTANGLE / 2);
        }
        else if (msg.uMsg == WM_RBUTTONUP) {
            // 鼠标右键点击时绘制一个圆
            circle(msg.x, msg.y, RADIUS_CIRCLE);
        }
    }

    return 0;
}

// ============================
#include <graphics.h>
#include <windows.h>

using namespace std;

// 地图高宽
#define Width 640
#define Height 480

#define WIDTH_RECTANGLE 10
#define HEIGHT_RECTANGLE 10
#define RADIUS_CIRCLE 10

typedef struct _Pos {
    int x;
    int y;
}Pos;

int main()
{
    bool isDrawPixel = false;
    bool isDrawRectangle = false;
    Pos firstPos;
    MOUSEMSG msg;           // 定义鼠标消息

    initgraph(Width, Height);
    
    while (true) {
        msg = GetMouseMsg();  // 获取鼠标消息

        if (msg.uMsg == WM_LBUTTONDOWN) {
            // 开始绘制点
            isDrawPixel = true;
        }
        else if (msg.uMsg == WM_LBUTTONUP) {
            // 结束绘制点
            isDrawPixel = false;
        }
        else if (msg.uMsg == WM_RBUTTONDOWN) {
            // 开始预绘制矩形
            isDrawRectangle = true;
            firstPos.x = msg.x;
            firstPos.y = msg.y;
        }
        else if (msg.uMsg == WM_RBUTTONUP) {
            // 绘制最终矩形
            if (isDrawRectangle) {
                setcolor(WHITE);
                rectangle(firstPos.x, firstPos.y, msg.x, msg.y);

                isDrawRectangle = false;
                firstPos.x = 0;
                firstPos.y = 0;
            }
        }

        // 绘制
        if (isDrawPixel) {
            putpixel(msg.x, msg.y, WHITE);
        }
        if (isDrawRectangle) {
            // 预绘制矩形
            setcolor(WHITE);
            rectangle(firstPos.x, firstPos.y, msg.x, msg.y);

            // 清除预绘制矩形
            setcolor(BLACK);
            rectangle(firstPos.x, firstPos.y, msg.x, msg.y);
        }
    }

    return 0;
}